package cn.junki.sparkbigmodeldemo.entity.vo;

import cn.hutool.json.JSONUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 
 * 接口json结果封装
 * 
 * @author Junki
 * @date 2022-01-10 17:01:39
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class JsonResult implements Serializable {

    private Integer code;

    private String message;

    private Object data;

    public static JsonResult success(String message, Object data) {
        return JsonResult.builder().code(200).message(message).data(data).build();
    }

    public static JsonResult success(Object data) {
        return JsonResult.builder().code(200).message("请求成功").data(data).build();
    }

    public static JsonResult success() {
        return JsonResult.builder().code(200).message("请求成功").build();
    }

    public static JsonResult fail(Integer code, String message) {
        return JsonResult.builder().code(code).message(message).build();
    }

    public static JsonResult fail() {
        return JsonResult.builder().code(500).message("请求失败").build();
    }

    public boolean isSuccess() {
        return this.code == 200;
    }

    public String toJsonStr() {
        return JSONUtil.toJsonStr(this);
    }

}
